package com.jintoufs.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.jintoufs.domain.user.User;
import com.jintoufs.util.RequestUtil;
import com.jintoufs.util.ResponseUtil;

public class LoginInterceptor extends BaseInterceptor implements HandlerInterceptor {
	private String loginUrl;

	/**
	 * 
	 * @param loginUrl
	 */
	public void setLoginUrl(String loginUrl) {
		this.loginUrl = loginUrl;
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		User user= (User) request.getSession().getAttribute("user");
		if (user!=null) {
			request.setAttribute("code", user.getUserName());
			request.setAttribute("name", user.getRealName());
		} else {
			if(RequestUtil.isAjaxRequest(request)){
				ResponseUtil.writeInfo(response, "loginTimeOut");
			}else{
				response.sendRedirect(request.getContextPath()+"/login.do");
			}
			return false;
		}

		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
	}

}
